Module modWRDBImport
    Public Const RegApp As String = "WRDBImport"

    Public Const Version As Double = 6.0#

    Public Const Import_Text_File As Short = 353

    Friend ProgressForm As frmProgress

    Friend SelectedPCode As String = "" 'used when importing multiple GIS USGS files, so don't have to reenter pcode

    ''' <summary>
    ''' WRDB project being imported into
    ''' </summary>
    Friend WRDBProject As WRDB.Project.clsProject

    Sub FatalError(ByRef msg As String)
        FileClose()
        MsgBox(msg.Replace("\n", vbCr))
    End Sub

    Sub PrintText(Optional ByVal Message As String = "")
        With ProgressForm.TextBox
            .AppendText(Message.Replace("\n", vbCrLf) & vbCrLf)
            .DeselectAll()
            .SelectionStart = .Text.Length
            .ScrollToCaret()
        End With
    End Sub

    Sub PrintText(ByVal FormatStr As String, ByVal ParamArray Arg() As Object)
        PrintText(String.Format(FormatStr, Arg))
    End Sub

    Sub PrintWarning(ByVal s As String, ByRef NumWarnings As Integer)
        NumWarnings = NumWarnings + 1
        If NumWarnings = 500 Then
            PrintText(s.Replace("\n", vbCr))
            PrintText("")
            PrintText("(No more than 500 of these warnings will be displayed...)")
            PrintText("")
        ElseIf NumWarnings < 500 Then
            PrintText(s)
        End If
    End Sub

    Sub PrintWarning(ByRef NumWarnings As Integer, ByVal FormatStr As String, ByVal ParamArray Arg() As Object)
        PrintWarning(String.Format(FormatStr, Arg), NumWarnings)
    End Sub

    Function Find(ByRef Target As String, ByRef a() As String, ByRef NumItems As Integer) As Integer
        'binary search algorithm
        Dim j, i, k As Integer
        Dim pad As String
        If NumItems = 0 Then Find = 0 : Exit Function
        pad = New String(" ", Len(Target)) 'must pad so string comparison is done correctly
        i = 1
        j = NumItems + 1
        Do
            k = (i + j) \ 2
            If Target < a(k) & pad Then j = k Else i = k
        Loop Until Target = a(k) Or (i >= j - 1 And k = i)
        Find = IIf(Target = a(k), k, 0)
    End Function

    Sub RippleSortString(ByRef a() As String, ByRef NumItems As Integer)
        Dim s As String
        Dim i, j As Integer
        For i = NumItems To 2 Step -1
            For j = 2 To i
                If a(j - 1) > a(j) Then s = a(j - 1) : a(j - 1) = a(j) : a(j) = s
            Next j
        Next i
    End Sub

    ''' <summary>
    ''' Given semicolon separated list of items, find case-insensitive match to target and return item number (0 if not found)
    ''' </summary>
    ''' <param name="Target">String searching for</param>
    ''' <param name="List">Semi-colon separated list of items to search</param>
    ''' <param name="Sorted">Set to True if list is sorted for faster search</param>
    ''' <returns>1-based index of item found</returns>
    Public Function ListItem(ByVal Target As String, ByVal List As String, Optional ByVal Sorted As Boolean = False) As Integer

        Target = Target.Trim.ToUpper
        List = TestNull(List, "").ToUpper

        'do a quick check to see if it is in there...
        If Not (";" & List & ";").Contains(";" & Target & ";") Then Return 0
        'If InStr(";" & List & ";", ";" & Target & ";") = 0 Then ListItem = 0 : Exit Function

        Dim ar() As String = Split(List, ";")
        If Sorted Then
            Dim i As Integer = Array.BinarySearch(ar, Target)
            If i = -1 Then Return 0 Else Return i + 1
        Else
            For i As Integer = UBound(ar) To LBound(ar) Step -1
                If ar(i) = Target Then Return i + 1
            Next i
            Return 0
        End If

    End Function

    ''' <summary>
    ''' Fill treeview control with nodes using semi-colon-separated list
    ''' </summary>
    ''' <param name="TreeView">Control to add nodes to</param>
    ''' <param name="TreeViewList">List of nodes; sublist starts with > and ends with 'less than sign'</param>
    Friend Sub FillTreeView(ByRef TreeView As TreeView, ByVal TreeViewList As String)
        With TreeView
            .Nodes.Clear()
            Dim ar() As String = Split(TreeViewList, ";")
            Dim ParentNode As TreeNode = Nothing
            For i As Integer = 0 To UBound(ar)
                If ar(i) = ">" Then 'first item of new sublist
                    ParentNode = .Nodes(.Nodes.Count - 1)
                ElseIf ar(i) = "<" Then 'last item of sublist, next parent is grandparent
                    ParentNode = ParentNode.Parent
                Else
                    If ParentNode Is Nothing Then
                        .Nodes.Add("Node " & .Nodes.Count, ar(i))
                    Else 'add to parent
                        ParentNode.Nodes.Add("Node " & .Nodes.Count, ar(i))
                    End If
                End If
            Next i
        End With
    End Sub

    Private Declare Auto Function GetShortPathName Lib "kernel32.dll" (ByVal path As String, ByVal shortname As System.Text.StringBuilder, ByVal maxlen As Integer) As Integer
    Public Function GetShortName(ByVal path As String) As String
        Dim sb As New System.Text.StringBuilder(259)
        Dim retval As Integer = GetShortPathName(path, sb, 259)
        If retval = 0 Then Throw New IO.IOException("Could not retrieve short name", retval)
        Return sb.ToString()
    End Function

End Module